<?php
/*
    [GRIRMS] (C) 2009 Grandison & JM Technologies

    @author kevin<hasson.lai@mobile-grandison.com>
    @version $Id: common.inc.php v1 2009-09-16 kevin $
*/
error_reporting(E_ALL ^ E_NOTICE);
//error_reporting(0);
session_start();
@set_magic_quotes_runtime(0);
$mtime = explode(' ', microtime());
$starttime = $mtime[1] + $mtime[0];

define('IN_GRIRMS', true);
define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());

define('APP_ROOT', substr(dirname(__FILE__), 0, -7));
define('APP_CACHEDIR', APP_ROOT.'/cache/');
define('ATT_DIR',APP_ROOT.'/attachment');

define('APP_URL',dirname($_SERVER['SCRIPT_NAME']));
define('MOD_URL',APP_URL.'/module');
define('IMG_URL',APP_URL.'/images');
define('CSS_URL',APP_URL.'/css');
define('JS_URL',APP_URL.'/js');
define('ATT_URL',APP_URL.'/attachment');

if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
}
$timestamp = time();

if (isset($_REQUEST['GLOBALS']) OR isset($_FILES['GLOBALS'])) {
	exit('Request tainting attempted.');
}

require_once APP_ROOT.'./include/global.func.php'; //通用
require_once APP_ROOT.'./include/cache.func.php'; //缓存
require_once APP_ROOT.'./include/db.func.php'; //数据库相关

//PATHINFO模式读取变量
if (!empty($_SERVER['PATH_INFO'])) {

    $var_array = explode('/', substr(str_replace('.shtml', '', $_SERVER['PATH_INFO']), 1));
    //print_r($var_array);
    $var_nums = count($var_array);
    $var_array && $_GET['m'] = $var_array[0];
    $var_array && $_GET['p'] = $var_array[1];
    
    if ($var_nums > 2) {
        for ($i = 2; $i < $var_nums; $i++) {
            $_GET[trim($var_array[$i])] = trim($var_array[++$i]);
        }
    }
}

foreach(array('_COOKIE', '_POST', '_GET') as $_request) {
	foreach($$_request as $_key => $_value) {
		$_key{0} != '_' && $$_key = daddslashes($_value);
	}
}

if (!MAGIC_QUOTES_GPC && $_FILES) {
	//$_FILES = daddslashes($_FILES);
}

//defined('NOCHARSET') || header("Content-type: text/html; charset=utf-8");

$config = parse_ini_file(APP_ROOT.'./config.ini.php', true);

$cookiepre = $config['cookie']['cookiepre'];
$cookiedomain = $config['cookie']['cookiedomain'];
$cookiepath = $config['cookie']['cookiepath'];
$authkey = $config['cookie']['authkey'];
$prelength = strlen($cookiepre);

$table_prefix = $config['database']['prefix'];

$web_title = $config['web']['title'];

foreach($_COOKIE as $key => $val) {
	if(substr($key, 0, $prelength) == $cookiepre) {
		$_DCOOKIE[(substr($key, $prelength))] = MAGIC_QUOTES_GPC ? $val : daddslashes($val);
	}
}
unset($prelength, $_request, $_key, $_value);


require_once APP_ROOT.'./include/db_mysql.class.php';

$db = new db_mysql();
$db->connect($config['database']['server'], $config['database']['uid'], $config['database']['pwd'], $config['database']['dbname'], $config['database']['pconnect'], true, $config['database']['charset']);
$config['database']['uid'] = $config['database']['pwd'] = $config['database']['pconnect'] = NULL;

$tplrefresh = $config['template']['tplrefresh'] ? 1 : 0;
$tpldir = trim($config['template']['tpldir']);

$authkey = md5($authkey.$_SERVER['HTTP_USER_AGENT']);

list($_userid, $_userpwd) = empty($_DCOOKIE['auth']) ? array(0, '') : daddslashes(explode("\t", authcode($_DCOOKIE['auth'], 'DECODE')), 1);

$_userid = intval($_userid);
$_username = '';
$userdata = array();
$_dealerid = 0;

//echo $_userid,',',$_userpwd;

if($_userid > 0){
    $userdata = $db->fetch_first("SELECT `username`,`admin_type`,`rank_type`,`give_type`,`status` FROM `system_user` WHERE `id`='{$_userid}' and `password`='{$_userpwd}' LIMIT 0,1");
    if($userdata){
        $_username = trim($userdata['username']);
        $_usertype = intval($userdata['usertype']);
        switch($userdata['status']){
            case 'OK':
                $userdata['upload'] = array(
                    'flg' => true,
                    'dir' => ATT_DIR.'/upload/'.$_userid,
                    'url' => ATT_URL.'/upload/'.$_userid,
                );
                break;
            case 'DISABLED':
                clearcookies();
                showmessage('对不起，您的帐户已被禁用！', APP_URL.'index.php');
                break;
            case 'DEL':
                clearcookies();
                showmessage('对不起，您的帐户已被删除！', APP_URL.'index.php');
                break;
        }
    }else{
        clearcookies();
    }
}
//print_r($userdata);

$base_name =explode('.php', basename($_SERVER['REQUEST_URI']));
$base_name=$base_name[0];
$this_script = strrchr($_SERVER['SCRIPT_NAME'], '/');
$this_script = substr($this_script, 1, strpos($this_script, '.') - 1);

//获取站点设置
$system_settings = get_settings();
//读取区域缓存
$category_list = cache_read('category_list');
$category_tree = cache_read('category_tree');
//读取用户类别缓存
$usertype_list = cache_read('user_type');

//For page
$prev_page_nums = 3;
$next_page_nums = 3;

//print_r($_COOKIE);
//print_r($_COOKIE);

?>
